package com.chenxingnet.service.Impl;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.UUID;

import javax.annotation.Resource;
import javax.imageio.stream.FileImageOutputStream;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import org.springframework.ui.Model;
import org.springframework.web.multipart.MultipartFile;

import com.chenxingnet.common.CommonString;
import com.chenxingnet.dao.DriversMapper;
import com.chenxingnet.dto.Drivers;
import com.chenxingnet.service.DriversService;
import com.chenxingnet.tools.CreateFileUtil;
import com.chenxingnet.tools.MobilePage;
import com.chenxingnet.tools.Page;
@Service("driversService")
public class DriversServiceImpl implements DriversService{
	@Resource
	private DriversMapper driversMapper;

	public int addDrivers(Drivers drivers) {
		return driversMapper.insertSelective(drivers);
	}

	public int deleteDrivers(Integer dId) {
		
		return driversMapper.updateById(dId);
	}

	
	public Drivers selectById(Integer dId) {
		
		return driversMapper.selectByPrimaryKey(dId);
	}

	public int updateDrivers(Drivers drivers) {
		
		return driversMapper.updateByPrimaryKeySelective(drivers);
	}

	public void selectAllByCount(HttpServletRequest request, Model model) {
		
		String pageNow = request.getParameter("pageNow");
		Page page = null;
		List<Drivers> driverList = new ArrayList<Drivers>();
		//按照名字搜索
		String name = request.getParameter("dname");
		if(name != null){
			List<Drivers> driverLists = driversMapper.selectByName(name);
			model.addAttribute("driverList", driverLists);
			request.setAttribute("driverList", driverLists);
		}else{
			//分页查询
			int totalCount = (int)driversMapper.getCount();
			if(pageNow != null){
				page = new Page(totalCount, Integer.valueOf(pageNow));
				driverList = this.driversMapper.selectAllByCount(page.getStartPos(), page.getPageSize());
			}else{
				page = new Page(totalCount, 1);
				driverList = this.driversMapper.selectAllByCount(page.getStartPos(), page.getPageSize());
			
			}
			
			model.addAttribute("driverList", driverList);
			model.addAttribute("page", page);
			request.setAttribute("driverList", driverList);
			request.setAttribute("page", page);
			
		}
		
		
	}

	/**
	 * 移动端分页查询
	 */
	public void mobSelectAllByCount(HttpServletRequest request) {
		
		String pageNow = request.getParameter("pageNow");
		MobilePage mobilePage = null;
		List<Drivers> driverList = new ArrayList<Drivers>();
		//按照名字搜索
		String name = request.getParameter("dname");
		if(name != null){
			List<Drivers> driverLists = driversMapper.selectByName(name);
			request.setAttribute("driverList", driverLists);
		}else{
			//分页查询
			int totalCount = (int)driversMapper.getCount();
			if(pageNow != null){
				mobilePage = new MobilePage(totalCount, Integer.valueOf(pageNow));
				driverList = this.driversMapper.selectAllByCount(mobilePage.getStartPos(), mobilePage.getPageSize());
			}else{
				mobilePage = new MobilePage(totalCount, 1);
				driverList = this.driversMapper.selectAllByCount(mobilePage.getStartPos(), mobilePage.getPageSize());
			
			}
			
			request.setAttribute("driverList", driverList);
			request.setAttribute("mobilePage", mobilePage);
			
		}
		
		
	}
	public String saveImg(MultipartFile m,String type, int index) {
		
		File directory = new File("");// 参数为空
		String   filedir=  "";
		 String  returnDir="";
		FileImageOutputStream imageOutput =null;
		
		 try {
			 byte[] bytes = m.getBytes();
			 /*
			  * 思路
			  * 1，创建文件夹
			  * 2，存储图片写入硬盘
			  * 3，返回路径
			  * */
			filedir= directory.getCanonicalPath();
			filedir=filedir.replace("bin","");
			filedir=filedir+"\\webapps\\huayang";
			String  destDirName=filedir+"\\HuaYangImg\\";
			Calendar now = Calendar.getInstance();  
			int   month=now.get(Calendar.MONTH)+1;
			String   datePath="\\"+now.get(Calendar.YEAR)+"\\"+month+"\\"+now.get(Calendar.DAY_OF_MONTH)+"\\";  
			CreateFileUtil.createDir(destDirName+datePath);
			UUID uuid = UUID.randomUUID();
			filedir= filedir+"\\HuaYangImg\\"+datePath+uuid+"_"+index+"."+type;
			imageOutput = new FileImageOutputStream(new File(filedir));
			imageOutput.write(bytes, 0, bytes.length); 
			imageOutput.close();
			returnDir=CommonString.httpHeadName+"/"+CommonString.projectName+"/"+CommonString.userTalkImg+"/"+now.get(Calendar.YEAR)+"/"+month+"/"+now.get(Calendar.DAY_OF_MONTH)+"/"+uuid+"_"+index+"."+type;
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}  
		 
		return returnDir;
	}

	public List<Drivers> selectAll() {
		return driversMapper.selectAll();
	}


}
